<?php
define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
include_once(ROOT_PATH . 'includes/lib_form.php');

header("Content-type: text/html; charset=".$charset);
$siteUrl='data/form';
$action  = isset($action) ? $action : 'formlist';
if($action == 'delform') {
	$datalist = $GLOBALS['db']->getAll("SELECT id FROM ".$ecs->table('form_data')." WHERE fid=$fid ORDER BY id DESC");
	$typelist = $GLOBALS['db']->getAll("SELECT id FROM ".$ecs->table('form_type')." WHERE fid='$fid' ORDER BY id DESC");
	if(count($datalist)) {
		foreach($datalist AS $v) {
			$dataid[] = $v['id'];
		}
		$dataid = implode(",", $dataid);
		$dataid = preg_replace("/([\d]+)/", "'\\1'", $dataid);
		$GLOBALS['db']->query("DELETE FROM ".$ecs->table('form_data')." WHERE id IN ($dataid)");
	}
	if(count($typelist)) {
		foreach($typelist AS $t) {
			$typeid[] = $t['id'];
		}
		$typeid = implode(",", $typeid);
		$typeid = preg_replace("/([\d]+)/", "'\\1'", $typeid);
		$GLOBALS['db']->query("DELETE FROM ".$ecs->table('form_type')." WHERE id IN ($typeid)");
	}
	if(file_exists("js/$fid.js")) {
		@unlink("js/$fid.js");
	}
	$GLOBALS['db']->query("DELETE FROM ".$ecs->table('form')." WHERE fid='$fid'");
	$links = array(
        array('href' => 'form.php?action=formlist', 'text' => '返回列表')
    );
	sys_msg('表单已删除', 0, $links);
} elseif ($action == 'deloption') {
	if($id && $fid) {
	$GLOBALS['db']->query("DELETE FROM ".$ecs->table('form_type')." WHERE fid='$fid' AND id='$id'");
	}
	$links = array(
        array('href' => 'form.php?action=listoption&fid='.$fid, 'text' => '返回表单选项')
    );
	sys_msg('表单选项已删除', 0, $links);

} elseif ($action == 'display') {
	$f = $GLOBALS['db']->getRow("SELECT * FROM ".$ecs->table('form')." WHERE fid='$fid'",true);
	$display = $f['display'] ? 0 : 1;
	$GLOBALS['db']->query("UPDATE ".$ecs->table('form')." SET display='$display' WHERE fid='$fid'");
	if($display == 0){
		if(file_exists("js/$fid.js")) {
			@unlink("js/$fid.js");
		}
		echo "启用";
	} else {
		echo "禁用";
	}

} elseif ($action == 'delmsg') {
	if(count($del)) {
		$delid = implode(",", $del);
		$delid = preg_replace("/([\d]+)/", "'\\1'", $delid);
		$GLOBALS['db']->query("DELETE FROM ".$ecs->table('form_data')." WHERE id IN ($delid)");
	}
	$links = array(
        array('href' => 'form.php?action=list&fid='.$fid, 'text' => '返回表单选项')
    );
	sys_msg('表单内容已删除', 0, $links);

} elseif ($action == 'saveform') {
	if(!$content['fname']) {
		sys_msg("表单名称不能为空");
	} else {
		$fname = $content['fname'];
		$fmsg = $content['fmsg'];
		$fid = $content['fid'];
		$addtime = time();
			$links = array(
		        array('href' => 'form.php?action=formlist', 'text' => '返回列表')
		    );
		if($fid) {
			$GLOBALS['db']->query("UPDATE ".$ecs->table('form')." SET fname='$fname', fmsg='$fmsg' WHERE fid='$fid'");
			sys_msg('更新完成',0,$links);
		} else {
			$GLOBALS['db']->query("INSERT INTO ".$ecs->table('form')." (fname,fmsg,addtime) VALUES ('$fname', '$fmsg', '$addtime')");
			sys_msg('添加完成',0,$links);
		}
	}
} elseif ($action == 'makjs') {
	if($fid) {
		$jsStr = @file_get_contents($GLOBALS['ecs']->get_domain() . "/form.php?fid=".$fid);
		$jsStr = str_replace('action="?action=savecontent"', "action=\"".$siteUrl."/index.php?action=savecontent\"", $jsStr);
		$jsStr = str_replace('"', '\"', $jsStr);
		$jsArr = array();
		$jsArr = explode("\r|\n", $jsStr);
		$jsContent = '';
		foreach($jsArr AS $val) {
			if($val) {
				$jsContent .= "document.writeln(\"".$val."\");\n";
			}
		}
		$path=ROOT_PATH.'data/form/js/'.$fid.'.js';
		createFolder(ROOT_PATH.'data/form/js'); 
		$fp = @fopen($path, 'w');
		if($fp) {
			@fwrite($fp, $jsContent);
			echo '更新完成';
		}
	}
} elseif ($action == 'saveaddoption') {
	if($fid && $type && $title) {
		$GLOBALS['db']->query("INSERT INTO ".$ecs->table('form_type')." (fid,orderid,type,title,msg,options,defaultvalue,ismust) VALUES ('$fid', '$orderid', '$type', '$title', '$msg', '$options', '$defaultvalue', '$ismust')");
		$links = array(
	        array('href' => 'form.php?action=listoption&fid='.$fid, 'text' => '返回表单选项')
	    );
		sys_msg('添加完成',0,$links);
	}
} elseif ($action == 'saveeditoption') {
	if($type && $title) {
		$GLOBALS['db']->query("UPDATE ".$ecs->table('form_type')." SET orderid='$orderid',type='$type',title='$title',msg='$msg',options='$options',defaultvalue='$defaultvalue',ismust='$ismust' WHERE id='$id'");
		$links = array(
	        array('href' => 'form.php?action=listoption&fid='.$fid, 'text' => '返回表单选项')
	    );
		sys_msg('修改成功',0,$links);
	}
}

$tophtml = <<< EOT
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link href="form/style.css" rel="stylesheet" type="text/css" />
<title>表单管理</title>
</head>
<body>
<table width="100%" class="top"><tr><td><a href="?action=formlist">表单管理</a></td></tr></table>
<SCRIPT LANGUAGE="JavaScript" src="form/js/jquery.js"></SCRIPT>
<SCRIPT LANGUAGE="JavaScript" src="form/js/check.js"></SCRIPT>
EOT;

$foothtml = <<< EOT
<div align="center">Powered By KnifeCMS.PHP 2.0</div>
<div id="wait"></div>
</body>
</html>
EOT;

 if ($action == 'list') {
	echo $tophtml;
	$pageSize = 20;
	$currepage = ($_GET['page'] == '') ? 1 : intval($_GET['page']);
	$start = ($currepage - 1) * $pageSize;
	$res = $GLOBALS['db']->query("SELECT * FROM ".$ecs->table('form_data')." WHERE fid='$fid'");
	$num = $GLOBALS['db']->num_rows($res);
	$list = $GLOBALS['db']->getAll("SELECT * FROM ".$ecs->table('form_data')." WHERE fid='$fid' ORDER BY id DESC LIMIT $start,$pageSize");
	echo showForm('formhead','?action=delmsg&fid='.$fid);
	echo '<table class="listtable" width="100%"><tr><td width="10%">删</td><td width="85%" align="center">内容</td></tr>';
	foreach($list AS $key=>$v) {
		$addtime = date("Y-m-d", $list[$key]['addtime']);
		$c = unserialize($v['content']);
		echo '<tr><td width="5%"><input name="del[]" type="checkbox" value="'.$list[$key]['id'].'"></td><td width="90%"><table width="100%">';
		if(count($c['title'])) {
			foreach($c['title'] AS $k => $title) {
				$content = $c['content'][$k];
				if(is_array($content)) {
					$content = implode(',', $content);
				}
				$content = str_replace("\r\n","<br>",$content);
				echo '<tr><td></td><td width="10%">'.$title.'</td><td width="90%">'.$content.'</td></tr>';
				
			}
		}
		echo '<tr><td></td><td width="10%"><b>提交日期</b></td><td width="90%">'.$addtime.'</td></tr></table>';

	}
	echo '</td></tr><tr><td width="10%"><input type="button" id="checkall1" value="全选"> <input type="button" id="checktog1" value="反选"></td><td width="90%"> <input type="submit" name="delmsg" value="删除所选">';
	echo '</td></tr></table>';
	echo '<div align="center">'.multi($num, $pageSize, $currepage,'admin.php?action=list&fid='.$fid).'</div><br />';
	echo $foothtml;
} elseif ($action == 'formlist') {
	echo $tophtml;
	$formlist = $GLOBALS['db']->getAll("SELECT * FROM ".$ecs->table('form')." ORDER BY fid DESC");
	echo '<table class="listtable" width="100%"><tr><td width="5%">fid</td><td width="10%">表单名称</td><td width="75%" align="center">操作</td><td width="10%">添加时间</td></tr>';
	foreach($formlist as $key => $form) {
		$fid = $form['fid'];
		$fname = $form['fname'];
		$display = $form['display'];
		$addtime = date("Y-m-d", $form['addtime']);
		$display = $display ? '禁用' : '启用';
		echo '<tr><td width="5%">'.$fid.'</td><td width="10%"><a target="_blank "href="/form.php?fid='.$fid.'">'.$fname.'</a></td><td width="75%" align="center">';
		echo '<a href="?action=formlist&fid='.$fid.'">修改表单</a> | <a href="?action=list&fid='.$fid.'">查看内容</a> | ';
		echo "<a href=\"?action=listoption&fid=$fid\">选项列表</a> ｜ <a id=\"display_$fid\" href=\"###\" onclick=\"changeform('display',$fid)\">$display</a> | <a href=\"?action=delform&fid=$fid\">删除</a> | <a href=\"###\" onclick=\"changeform('makjs',$fid)\">更新JS</a><a class=\"showmsg\" id=\"makjs_$fid\"></a> | <a id=\"showjs_$fid\" href=\"#\" onclick=\"showjs($fid)\">调用代码</a><br /><a id=\"jshtml_$fid\" style=\"display:none\">".MooHtmlspecialchars('<SCRIPT LANGUAGE="JavaScript" src="'.$siteUrl.'/js/'.$fid.'.js"></SCRIPT>')."</a></td>";
		echo '<td width="10%">'.$addtime.'</td></tr>';

	}
	$editFid = intval($_GET['fid']);
	if($editFid) {
		$editForm = $GLOBALS['db']->getRow("SELECT * FROM ".$ecs->table('form')." WHERE fid='$editFid'",true);
	}
	echo '</table>';
	echo '<br /><h3>添加/修改表单</h3><table class="listtable" width="100%">';
	echo showForm('formhead', '?action=saveform');
	echo showForm('text', '表单名称', '', $editForm['fname'], '', 'fname');
	echo showForm('textarea', '表单说明', '', $editForm['fmsg'], '', 'fmsg');
	echo showForm('hidden', 'fid', $editForm['fid']);
	echo showForm('submit', 'saveform').'</td></tr></table>';
	echo $foothtml;

} elseif ($action == 'editoption') {
	echo $tophtml;
	$optionmsg = $GLOBALS['db']->getRow("SELECT * FROM ".$ecs->table('form_type')." WHERE id='$id'",true);
	$type = $optionmsg['type'];
	$ismust = $optionmsg['ismust'];
	if($type == 'text') {
		$stext = '单行文本(text)';
		$diplay = 'display:none';
		$sdiplay = '';
	}
	if($type == 'textarea') { 
		$stext = '多行文本(textarea)';
		$diplay = "display:none";
		$sdiplay = '';
	}
	if($type == 'select') {
		$stext = '下拉框(select)';
		$diplay = "";
		$sdiplay = "display:none";
	}
	if($type == 'radio') {
		$stext = '单选框(radio)';
		$diplay = "";
		$sdiplay = "display:none";
	}
	if($type == 'checkbox') {
		$stext = '多选框(checkbox)';
		$diplay = "";
		$sdiplay = "display:none";
	}
	if($type == 'pass') {
		$stext = '密码框(password)';
		$diplay = 'display:none';
		$sdiplay = "";
	}
	if($type == 'file') {
		$stext = '上传文件(file)';
		$diplay = 'display:none';
		$sdiplay = "";
	}

	if($type == 'hidden') {
		$stext = '隐藏域(hidden)';
		$diplay = 'display:none';
		$sdiplay = "display:none";
	}

	if($ismust == 'must') {
		$itext = '不能为空';
	}
	if($ismust == 'email') {
		$itext = '邮件格式';
	}
	if($ismust == 'phone') {
		$itext = '电话号码';
	}
	if($ismust == 'mobile') {
		$itext = '手机号码';
	}
	if($ismust == 'url') {
		$itext = '网址格式';
	}
	if($ismust == 'number') {
		$itext = '数字格式';
	}
	if($ismust == 'post') {
		$itext = '邮政编码';
	}
	if($ismust == 'phone') {
		$itext = '手机格式';
	}
	if($ismust == 'qq') {
		$itext = 'QQ号码';
	}
	if($ismust == 'english') {
		$itext = '英文字母';
	}
	if($ismust == 'number') {
		$itext = '数字格式';
	}

	?>
<form action="?action=saveeditoption" method="post" name="myform">
<input type="hidden" name="id" value="<?=$optionmsg['id']?>">
<input type="hidden" name="fid" value="<?=$optionmsg['fid']?>">
<table cellpadding="0" cellspacing="0" border="0" width="100%" height="10">
	<tr>
		<td></td>
	</tr>
</table><table cellpadding="2" cellspacing="1" class="tableborder">
    <tr>
      <td class='tablerow'><strong>选项名称</strong></td>
      <td class='tablerow'>
          <input type="text" name="title" value="<?=$optionmsg['title']?>">
	  </td>
    </tr>
    <tr>
      <td class='tablerow'><strong>选项说明</strong></td>
      <td class='tablerow'>
          <input type="text" name="msg" size="50" value="<?=$optionmsg['msg']?>">
	  </td>
    </tr>
    <tr> 
      <td class="tablerow"><strong>选项类型</strong></td>
      <td class="tablerow">
<select name="type" id="type">
<option value='<?=$type?>' selected><?=$stext?></option>
<option value='text'>单行文本(text)</option>
<option value='textarea'>多行文本(textarea)</option>
<option value='select'>下拉框(select)</option>
<option value='radio'>单选框(radio)</option>
<option value='checkbox'>多选框(checkbox)</option>
<option value='password'>密码框(password)</option>
<option value='file'>上传文件(file)</option>
<option value='hidden'>隐藏域(hidden)</option>
</select>
	 </td>
    </tr>
    <tr id="default">
      <td class='tablerow'><strong>默认值</strong></td>
      <td class='tablerow'>
          <textarea name='defaultvalue' rows='1' cols='50' onkeypress="javascript:checktextarealength('defaultvalue',30);"><?=$optionmsg['defaultvalue']?></textarea>
	  </td>
    </tr>
    <tr id='trOptions' style='<?=$diplay?>'>
      <td  class='tablerow'><strong>表单选项：</strong><br>每行一个</td>
      <td class='tablerow'><textarea name='options' cols='40' rows='5' id='options'><?=$optionmsg['options']?></textarea></td>
    </tr>
       <tr>
      <td class='tablerow'><strong>排列顺序</strong></td>
      <td class='tablerow'>
          <input type="text" name="orderid" value="<?=$optionmsg['orderid']?>">
	  </td>
    </tr>
        <tr id='ismustselect' style='<?=$sdiplay?>'> 
      <td class="tablerow"><strong>内容限制</strong></td>
      <td class="tablerow">
<select name="ismust">
<option value='<?=$ismust?>' selected><?=$itext?></option>
<option value=''>不做限制</option>
<option value='must'>不能为空</option>
<option value='email'>邮件格式</option>
<option value='mobile'>手机号码</option>
<option value='phone'>电话号码</option>
<option value='url'>网址格式</option>
<option value='post'>邮政编码</option>
<option value='qq'>QQ号码</option>
<option value='number'>数字格式</option>
<option value='english'>英文字母</option>
</select>
	 </td>
    </tr>
    <tr> 
      <td class="tablerow"></td>
      <td class="tablerow"> <input type="submit" name="submit" value=" 确定 "> 
        &nbsp; <input type="reset" name="reset" value=" 清除 "> </td>
    </tr>
  </form>
</table>
	<?php

	echo $foothtml;
} elseif ($action == 'listoption') {
	echo $tophtml;
	$optionlist = $GLOBALS['db']->getAll("SELECT * FROM ".$ecs->table('form_type')." WHERE fid='$fid' ORDER BY orderid ASC");
	echo '<table class="listtable" width="100%"><tr><td width="10%">选项名称</td><td width="80%" align="center">操作</td></tr>';
	foreach($optionlist AS $option) {
		$id = $option['id'];
		$fid = $option['fid'];
		$title = $option['title'];
		echo '<tr><td width="10%">'.$title.'</td><td width="80%" align="center">';
		echo '<a href="?action=editoption&id='.$id.'">修改选项</a> ｜ <a href="?action=deloption&fid='.$fid.'&id='.$id.'">删除</a></td></tr>';

	}
	echo '</table>';
	?>
<br />
<h3>添加选项</h3>
<form action="?action=saveaddoption" method="post" name="myform">
<input type="hidden" name="fid" value="<?=$fid?>">
<table cellpadding="0" cellspacing="0" border="0" width="100%" height="10">
	<tr>
		<td></td>
	</tr>
</table><table cellpadding="2" cellspacing="1" class="tableborder">
    <tr>
      <td class='tablerow'><strong>选项名称</strong></td>
      <td class='tablerow'>
          <input type="text" name="title" value="">
	  </td>
    </tr>
    <tr>
      <td class='tablerow'><strong>选项说明</strong></td>
      <td class='tablerow'>
          <input type="text" name="msg" size="50" value="">
	  </td>
    </tr>
    <tr> 
      <td class="tablerow"><strong>选项类型</strong></td>
      <td class="tablerow">
<select name="type" id="type">
<option value='text' selected>单行文本(text)</option>
<option value='textarea'>多行文本(textarea)</option>
<option value='select'>下拉框(select)</option>
<option value='radio'>单选框(radio)</option>
<option value='checkbox'>多选框(checkbox)</option>
<option value='password'>密码框(password)</option>
<option value='file'>上传文件(file)</option>
<option value='hidden'>隐藏域(hidden)</option>
</select>
	 </td>
    </tr>
    <tr id="default">
      <td class='tablerow'><strong>默认值</strong></td>
      <td class='tablerow'>
          <textarea name='defaultvalue' rows='1' cols='50' onkeypress="javascript:checktextarealength('defaultvalue',30);"></textarea>
	  </td>
    </tr>
    <tr id='trOptions' style='display:none'>
      <td  class='tablerow'><strong>表单选项：</strong><br>每行一个</td>
      <td class='tablerow'><textarea name='options' cols='40' rows='5' id='options'></textarea></td>
    </tr>
       <tr>
      <td class='tablerow'><strong>排列顺序</strong></td>
      <td class='tablerow'>
          <input type="text" name="orderid" value="255">
	  </td>
    </tr>
      <tr id="ismustselect"> 
      <td class="tablerow"><strong>内容限制</strong></td>
      <td class="tablerow">
	<select name="ismust">
		<option value=''>不做限制</option>
		<option value='must'>不能为空</option>
		<option value='email'>邮件格式</option>
		<option value='mobile'>手机号码</option>
		<option value='phone'>电话号码</option>
		<option value='url'>网址格式</option>
		<option value='post'>邮政编码</option>
		<option value='qq'>QQ号码</option>
		<option value='number'>数字格式</option>
		<option value='english'>英文字母</option>
	</select>
	 </td>
    </tr>
    <tr> 
      <td class="tablerow"></td>
      <td class="tablerow"> <input type="submit" name="submit" value=" 确定 "> 
        &nbsp; <input type="reset" name="reset" value=" 清除 "> </td>
    </tr>
  </form>
</table>

	<?php
	echo $foothtml;

}

?>
